---
title: "LSS"
author: "`r Sys.info()['user']`"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source("functions.R")
require(LSX)
require(quanteda)

lss <- readRDS("lss_en.RDS")
dfmt <- lss$data %>% 
    dfm_group()

dat <- docvars(dfmt)
dat$lss <- predict(lss, newdata = dfmt)
dat_sub <- dat[sample(nrow(dat), 10000),]
```

```{r, cache=TRUE}
dat_smooth <- smooth_lss(dat, lss_var = "lss", span = 0.05,
                         from = as.Date("1981-01-01"), 
                         to = as.Date("2008-12-31"))
```

```{r}
events <- list(
    "Savings & loan crisis" = "1989-11-17",
    "Asia economic crisis" = "1997-07-02",
    "Dot-com bubble burst" = "2000-04-14",
    "Subprime mortgage crisis" = "2007-01-28"
)
```


```{r fig.height=4.5, fig.width=10}
par(mar = c(2, 4, 1, 1))
plot(dat_sub$date, dat_sub$lss, ylim = c(-1, 1), col = rgb(0, 0, 0, 0.05), pch = 16,
     xlab = "", ylab = "Sentiment")
lines(dat_smooth$date, dat_smooth$fit + dat_smooth$se * 1.96, lty = 2)
lines(dat_smooth$date, dat_smooth$fit - dat_smooth$se * 1.96, lty = 2)
lines(dat_smooth$date, dat_smooth$fit)
abline(h = 0, lty = 1)
par(family = "Arial Narrow")
add_events(events)
```


